<?php
/* @var $this AnalysisController */

Yii::app()->clientScript->registerScript('search', "
$('.search-form form').submit(function(){
	$.fn.yiiGridView.update('product-grid', {
		data: $(this).serialize()
	});
	return false;
});
");
?>
<?php $form=$this->beginWidget('CActiveForm', array(
		'id'=>'proPromotion-form',
		'enableAjaxValidation'=>false,
	)); ?>
	<?php echo $form->hiddenField($model,'zcr_type')?>
	<ul class="form_list form_list2 discount create_box">
		<li class="c_f title"><h3 class="fs_14">创建限时折扣</h3></li>
		<li class="c_f"><span class="tit"><em class="xing">*</em>活动标题：</span>
			<div class="cont f_l">
				<?php echo $form->textField($model,'zcr_title',array('class'=>'t_ipt w_300'))?>
				<span id="CmpPromotion_zcr_titleTip"></span>
			</div>
		</li>
		<li class="c_f"><span class="tit"><em class="xing">*</em>活动时间：</span>
			<div class="cont f_l">
					<?php echo $form->textField($model,'zcr_start_time',array('onclick'=>"WdatePicker({dateFmt:'yyyy-MM-dd HH'})"))?>
					<span class="c_999">-</span>
					<?php echo $form->textField($model,'zcr_end_time',array('onclick'=>"WdatePicker({dateFmt:'yyyy-MM-dd HH'})"))?>
					<span id="CmpPromotion_zcr_start_timeTip"></span>
			</div>
		</li>
		<li class="c_f"><span class="tit"><em class="xing">*</em>折扣宝贝：</span>
			<div class="cont f_l">
				<div class="match_box">
					<table width="100%" id="select_product_list" cellpadding="0" cellspacing="0">
						<tr>
							<th width="2%"></th>
							<th width="" class="tl">宝贝信息</th>
							<th width="15%" class="t_c">原价</th>
							<th width="13%" class="t_c">折扣</th>
							<th width="13%" class="t_c">折后价</th>
							<th width="12%" class="t_c">操作</th>
							<th width="2%" class="t_c"></th>
						</tr>
						<tr class="no_add no_bd <?php echo empty($model->discount_product)?'d_n':''?>">
							<td colspan="7" class="c_999">未添加折扣宝贝</td>
						</tr>
						<tr class="topbar no_bd  <?php echo empty($model->discount_product)?'':'d_n'?>">
							<td colspan="7"><span class="fw_b">批量设置：</span><label class="c_666">折扣 <input class="t_ipt w_60 zpd_discount_all" type="text" name="" value="" />折</label></td>
						</tr>
						<?php if(!empty($model->discount_product)){ foreach($model->discount_product as $val){?>
							<tr class="prod" id="select_product_list_<?php echo $val->zp_id?>">
								<td class="no_border"><input type="hidden" name="ProDiscount[zp_id][]" value="<?php echo $val->zp_id?>"></td>
								<td class="pro tl"><div class="c_img c_img50"><?php echo CHtml::link(Tool::thumb($val->product->zp_pic,50,50,$val->product->zp_name),array('/home/product/detail','id'=>$val->product->zp_id))?></div>
								<p class="title_pro"><?php echo CHtml::link(mb_substr($val->product->zp_name,0,12,'utf8'),array('/home/product/detail','id'=>$val->product->zp_id))?></p>
								</td>
								<td class="price"><?php echo $val->product->zp_price?></td>
								<td><input class="t_ipt w_60 zpd_discount" type="text" name="ProDiscount[zpd_discount][]" value="<?php echo $val->zpd_discount?>" /></td>
								<td><strong class="fs_14 c_f60"><?php echo number_format($val->product->zp_price*$val->zpd_discount/10,2,'.','')?></strong></td>
								<td><a class="c_06c delete_select" href="javascript:" title="">删除</a></td><td class="no_border"></td>
							</tr>
						<?php }}?>
					</table>
				</div>
				<p><a class="mem_bgx link_btn mr10" onclick="return addmatch();" href="javascript:" title="">添加折扣宝贝<b class="mem_bgx left"></b><b class="mem_bgx right"></b></a></p>
			</div>
		</li>
		<li class="c_f save_link"><span class="tit">&nbsp;</span>
			<div class="cont f_l">
				<?php echo CHtml::submitButton(Yii::t('cmp','Save Promotion'),array('class'=>'mem_btn mem_btn20 mr5'))?>
				<?php echo CHtml::resetButton(Yii::t('cmp','Reset'),array('class'=>'mem_btn mem_btn07'))?>
			</div>
		</li>
	</ul>
<?php $this->endWidget(); ?>



<div class="d_n" id="product_list">
<div class="d-title" style="display: block; ">选择产品</div>
<a class="d-close" href="javascript:" style="display: block;" onclick="addmatch()">×</a>
<div class="pro_filter c_f search-form">
<?php $form=$this->beginWidget('CActiveForm', array(
	'action'=>Yii::app()->createUrl($this->route),
	'method'=>'get',
)); ?>
<label>产品标题：</label>
<?php echo $form->textField($product,'zp_name',array('class'=>'t_ipt w_180'))?>
<label>产品货号：</label>
<?php echo $form->textField($product,'pro_no',array('class'=>'t_ipt w_180'))?>
<?php echo CHtml::submitButton(Yii::t('cmp','Search'),array('class'=>'mem_bgx mem_btn01'))?>
<?php $this->endWidget(); ?>
</div>

<div style="height:300px;overflow-y:auto;">
<!-- 列表 -->
<?php 
	$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'product-grid',
	'dataProvider'=>$product->search(40),//数据源
	//'filter'=>$product,//设置过滤器，筛选输入框
	'template'=>'{items}',
	'enablePagination'=>false,
	'columns'=>array(
		array(
			'class' => 'CCheckBoxColumn',
			'checkBoxHtmlOptions' => array('name' => 'id[]','class'=>'chk d_n'),
			'htmlOptions'=>array('width'=>'6%'),
		),
		array('class'=>'CLinkColumn',
				'header'=>'产品图片',
				'labelExpression'=>'Tool::thumb($data->zp_pic,50,50,$data->zp_name)',
				'urlExpression'=>'array("/home/product/detail","id"=>$data->zp_id)',
				'linkHtmlOptions'=>array('width'=>'50','height'=>'50'),
				'htmlOptions'=>array('width'=>'10%'),
		),
		array('class'=>'CLinkColumn',
				'header'=>'产品标题',
				'labelExpression'=>'mb_substr($data->zp_name,0,12,"utf8")',
				'urlExpression'=>'array("/home/product/detail","id"=>$data->zp_id)',
				'linkHtmlOptions'=>array('class'=>'c_06c'),
				'htmlOptions'=>array('width'=>'30%','class'=>'t_c'),
		),
		array('name'=>'pro_no',
				'value'=>'$data->pro_no',
				'htmlOptions'=>array('width'=>'10%','class'=>'t_c'),
		),
		array('name'=>'zp_price',
				'value'=>'$data->zp_price',
				'htmlOptions'=>array('width'=>'15%','class'=>'t_c'),
		),
		array('name'=>'zp_inventory',
				'value'=>'$data->zp_inventory',
				'htmlOptions'=>array('width'=>'15%','class'=>'t_c'),
		),
		array('class'=>'CLinkColumn',
				'header'=>'操作',
				'labelExpression'=>'"选择"',
				'urlExpression'=>'"javascript:"',
				'linkHtmlOptions'=>array('class'=>'mem_btn mem_btn13 pro_item'),
				'htmlOptions'=>array('width'=>'14%'),
		),
	)
));
?>
</div>
<div class="d-footer">
<div class="t_l f_l" id="select_pro" style="padding:10px 20px;margin:10px auto;width:600px;max-height:50px;overflow-y:auto;">
</div>
<div class="d-buttons" style=""><input type="button" class="d-button d-state-highlight" value="确定" onclick="addmatch()"></div>
</div>
</div>

<script language="javascript" type="text/javascript" src="<?php echo Yii::app()->baseUrl;?>/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">

//选择
$('.pro_item').live('click',function(){
	var pid = $(this).parent().parent().find('.chk').val()
	var pic = $(this).parent().parent().find('td').eq(1).html();
	var pname = $(this).parent().parent().find('td').eq(2).html();
	var pnametext = $(this).parent().parent().find('td').eq(2).text();
	var pprice = $(this).parent().parent().find('td').eq(4).html();
	if($(this).text()=='删除'){
		$('#select_pro_'+pid).remove();
		$('#select_product_list_'+pid).remove();
		$(this).text('选择');
		$(this).removeClass('c_06c');
		$(this).addClass('mem_btn mem_btn13');
		$(this).prev('label').remove();
		if(!($('#select_product_list').find('.prod').eq(0).length>0)){
			$('#select_product_list .no_add').show();
			$('#select_product_list .topbar').hide();
		}
	}else{
	/*	if($('#select_product_list .prod').length>4){
			art.dialog({title:'警告：',content:'只能最多选择五件产品',okValue:"确定",ok:function(){return true;}});
		}else{*/
			if($('#select_product_list_'+pid).length>0){
				art.dialog({title:'警告：',content:'相同产品请勿重复添加',okValue:"确定",ok:function(){return true;}});
				$(this).text('删除');
				$(this).removeClass('mem_btn mem_btn13');
				$(this).addClass('c_06c');
				$(this).after('<label class="mr10">已选择</label>');
			}else{
				$('#select_pro').append('<span class="mr10 pro" id="select_pro_'+pid+'" pid="'+pid+'">'+pnametext+'</span>');
				$('#select_product_list').append('<tr class="prod" id="select_product_list_'+pid+'"><td class="no_border"><input type="hidden" name="ProDiscount[zp_id][]" value="'+pid+'"></td><td class="pro tl">'
												+'<div class="c_img c_img50">'+pic+'</div><p class="title_pro">'+pname+'</p>'
												+'</td><td class="price">'+pprice+'</td><td><input class="t_ipt w_60 zpd_discount" type="text" name="ProDiscount[zpd_discount][]" value="" /></td><td><strong class="fs_14 c_f60">'+pprice+'</strong></td><td><a class="c_06c delete_select" href="javascript:" title="">删除</a></td><td class="no_border"></td></tr>');
				$(this).text('删除');
				$(this).removeClass('mem_btn mem_btn13');
				$(this).addClass('c_06c');
				$(this).before('<label class="mr10">已选择</label>');
				$('#select_product_list .no_add').hide();
				$('#select_product_list').removeClass('error');
				$('#select_product_list .topbar').show();
			}
	//	}
	}
})

//显示弹框
function addmatch(){
	$('#product_list').toggle();
}

//删除选择
$('.delete_select').live('click',function(){
	var pid = $(this).parent().parent().find('input').val();
	$('#select_pro_'+pid).remove();
	$('#select_product_list_'+pid).remove();
	if(!($('#select_product_list').find('.prod').eq(0).length>0)){
		$('#select_product_list .no_add').show();
		$('#select_product_list .topbar').hide();
	}
});

//输入折扣
$('.zpd_discount').live('change',function(){
	var discount_price = $(this).parent().prev('td').text();
	//折扣格式化成一位小数
	var discount = ($(this).val()/1).toFixed(1);
	$(this).val(discount);
	if(discount<0.1||discount>10){
		art.dialog({title: '错误',content: '拆扣应该在0.1至10之前'}).time(1500);
		$(this).val('');
		$(this).addClass('error');
		return;
	}
	discount_price = (discount_price*$(this).val()/10).toFixed(2);
	$(this).parent().next('td').find('strong').text(discount_price);
});

//输入折扣
$('.zpd_discount_all').live('change',function(){
	//折扣格式化成一位小数
	var discount = ($(this).val()/1).toFixed(1);
	$(this).val(discount);
	if(discount<0.1||discount>10){
		art.dialog({title: '错误',content: '拆扣应该在0.1至10之前'}).time(1500);
		$(this).val('');
		$(this).addClass('error');
		return;
	}
	
	var num = $('.zpd_discount').length;
	$('.zpd_discount').val(discount);
	for(var i=0;i<num;i++){
		var discount_price = $('.zpd_discount').eq(i).parent().prev('td').text();
		discount_price = (discount_price*discount/10).toFixed(2);
		$('.zpd_discount').eq(i).parent().next('td').find('strong').text(discount_price);
	}
});

$(function(){
	$('#product_list').draggable({ handle: '.d-title' });
});

//提交前的验证，判断有没有填写生效条件
$('#proPromotion-form').bind("submit", function() {
    if($('#select_product_list').find('.prod').length<1){
		$('#select_product_list').addClass('error');
		return false;
    }
	var num = $('.zpd_discount').length;
	for(var i=0;i<num;i++){
	    if($('.zpd_discount').eq(i).val().length<1){
			$(this).addClass('error');
			return false;
	    }
	}
    return true;
});
</script>

 
<!-- 表单验证 -->
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl;?>/js/formvalidata/formValidator-4.1.1.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl;?>/js/formvalidata/formValidatorRegex.js"></script>
 <script>
	jQuery(document).ready(function($) {
		$.formValidator.initConfig({theme:"Default",formID:"proPromotion-form",
			onError:function(msg, obj){
					//	alert(msg);
						return false;
					},
			onsuccess:function(){ return true; }
		});
	})
</script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl;?>/js/formvalidata/cmpValidate.js"></script> 
 